/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import model.Familia;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author JONATAS
 */
public class FamiliaDAO {

    private Session session;
    // método construtor - igual ao nome da Familia precedido por public

    public FamiliaDAO() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    public void insert(Familia m) {
        Transaction t = session.beginTransaction();
        session.save(m);
        t.commit();
    }

    public void update(Familia m) {
        Transaction t = session.beginTransaction();
        session.merge(m);
        t.commit();
    }

    public void delete(Familia m) {
        Transaction t = session.beginTransaction();
        session.delete(m);
        t.commit();
    }

    public Familia findById(String id) {
        return (Familia) session.load(Familia.class, id);
    }

    public List<Familia> findAll() {        
        List<Familia> list = new ArrayList<Familia>();        
        try{
        return session.createCriteria(Familia.class).list(); // faz um select * from marca
        }catch(org.hibernate.ObjectNotFoundException ex){  
            System.out.println("CATCH!");
            return list;
        }        
    }
    
    public List<Familia> findByDescricao(String descricao) {
                
        Query q = session.createQuery( "from Familia where upper(fam_descricao) like upper(:X)" ); /* Professor acha melhor usar query e não SQL Query ->   equivalente a SELECT * FROM MARCA (nome da Familia) */  
        q.setParameter("X", "%"+descricao+"%");   /* seta o parametro */
        return q.list();
                
    }
    public List<Familia> findByClasse(String classe_id) {
        System.out.println("Find by classe --- "+classe_id);        
        Query q = session.createQuery( "from Familia where cla_id = :X");        
        q.setParameter("X", ""+classe_id);            
        return q.list();   
    }    
    
    public String getCode() {
        try{
        Query q = session.createSQLQuery("Select max(lpad(cast(fam_id as int)+1,2,0)) from Familia ");
        if(q.uniqueResult().toString().isEmpty())
            return "01";
        else if (Integer.parseInt(q.uniqueResult().toString()) == 99) {
            JOptionPane.showMessageDialog(null, "Limite de cadastro excedido!");
            return "ERRO!";
        } else {
            System.out.println("Results -> " + q.uniqueResult().toString());
            return q.uniqueResult().toString();
        }}catch(java.lang.NullPointerException ex){
            return "01";
        }
        
    }
}
